import 'dart:convert';

import 'package:explore/act/slide.dart';
import 'package:explore/tool/toolweb.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart' hide Page;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
import '../provider/person_info.dart';
import '../tool/inputbox.dart';
import 'package:http/http.dart' as http;

class Errorsign extends StatefulWidget {
  const Errorsign({super.key});

  @override
  State<StatefulWidget> createState() => _Errorsign();
}

class _Errorsign extends State<Errorsign> {
  List errorSign = [];
  List actSign = [];
  String selectvalue = "";
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      final personInfo = Provider.of<PersonInfo>(context, listen: false);
      var url = Uri.parse('http://127.0.0.1:5000/error_sign_show');
      var headers = {'Content-Type': 'application/json;charset=utf-8'};
      var body =
          jsonEncode({'alliance': personInfo.alliance, 'act': personInfo.act});
      final request = http.Request('get', url);
      request.headers.addAll(headers);
      request.body = body;
      request.send().then((value) {
        http.Response.fromStream(value).then((response) {
          setState(() {
            errorSign = jsonDecode(response.body)["msg"][0];
            actSign = jsonDecode(response.body)["msg"][1];
            selectvalue = actSign[0];
          });
        });
      }).catchError((value) {
        Fluttertoast.showToast(msg: '初始化异常');
      });
    });
  }

  void agree(PersonInfo person, String name) async {
    final url = Uri.parse('http://127.0.0.1:5000/error_sign_agree');
    final headers = {'Content-Type': 'application/json;charset=utf-8'};
    final body = jsonEncode(
        {'name': name, 'act': selectvalue, 'alliance': person.alliance});
    var response = await http.post(url, headers: headers, body: body);
    if (response.statusCode == 200) {
      Fluttertoast.showToast(msg: '修改成功');
      errorSign.remove(name);
      setState(() {});
    } else {
      Fluttertoast.showToast(msg: '修改失败');
    }
  }

  void setselectvalue(PersonInfo person, String newValue) {
    final personInfo = Provider.of<PersonInfo>(context, listen: false);
    var url = Uri.parse('http://127.0.0.1:5000/error_sign_show');
    var headers = {'Content-Type': 'application/json;charset=utf-8'};
    var body = jsonEncode({'alliance': personInfo.alliance, 'act': newValue});
    final request = http.Request('get', url);
    request.headers.addAll(headers);
    request.body = body;
    request.send().then((value) {
      http.Response.fromStream(value).then((response) {
        setState(() {
          errorSign = jsonDecode(response.body)["msg"][0];
          actSign = jsonDecode(response.body)["msg"][1];
          selectvalue = newValue;
        });
      });
    }).catchError((value) {
      Fluttertoast.showToast(msg: '初始化异常');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Toolweb(
        name: "行动补签",
        list: errorSign,
        agree: agree,
        actlist: actSign,
        selectvalue: selectvalue,
        setvalue: setselectvalue);
  }
}
